<template>
    <div>
        <!--   style="height: 840px;"    -->
        <el-row style="width: 100%">
            <el-row style="">
                <search-bar @onSearch="searchResult" ref="searchBar"></search-bar>
            </el-row>
            <!--            <view-switch class="switch"></view-switch>-->
            <br>
            <el-row style="width: 100%">
                <el-tooltip effect="dark" placement="right"
                            v-for="(item) in  books.slice((currentPage-1)*pagesize,currentPage*pagesize)"
                            :key="item.id">

                    <template #content>
                        <div>{{ item.title }}</div>
                        <span>{{ item.author }}</span>
                        <span>{{ item.date }}</span> /
                        <span>{{ item.press }}</span>
                        <br>
                        <div>{{ item.abs }}</div>
                    </template>
                    <!--                <p slot="content" style="font-size: 14px;margin-bottom: 6px;">{{ item.title }}</p>-->
                    <!--                <p slot="content" style="font-size: 13px;margin-bottom: 6px">-->
                    <!--
                                  </p>-->
                    <!--                <p slot="content" style="width: 300px" class="abstract">{{ item.abs }}</p>-->
                    <el-card style="width: 135px;margin-bottom: 20px;height: 233px;float: left;margin-right: 15px"
                             class="book"
                             bodyStyle="padding:10px" shadow="hover">
                        <div class="cover">
                            <img :src="item.cover" alt="封面">
                        </div>
                        <div class="info">
                            <div class="title">
                                <a href="">{{ item.title }}</a>
                            </div>
                        </div>
                        <div class="author">{{ item.author }}</div>
                    </el-card>
                </el-tooltip>
            </el-row>
        </el-row>
        <el-row>
            <el-pagination
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-size="pagesize"
                    :total="books.length">
            </el-pagination>
        </el-row>
    </div>
</template>

<script>
    import SearchBar from './SearchBar'
    // import ViewSwitch from './ViewSwitch'
    import {getCurrentInstance, onMounted, reactive, toRefs} from "vue";

    export default {
        name: 'Books',
        components: {
            SearchBar,
            // ViewSwitch
        },
        setup() {
            const data = reactive({
                books: [],
                currentPage: 1,
                pagesize: 18
            })
            let {proxy} = getCurrentInstance()

            onMounted(() => {
                loadBooks()
            })

            function loadBooks() {

                proxy.$axios.get('/books').then(resp => {
                    // console.info(resp)
                    if (resp && resp.status === 200) {
                        // console.info(resp.data)
                        data.books = resp.data.result
                        console.log(data.books)
                    }
                })
            }

            function handleCurrentChange(currentPage) {
                data.currentPage = currentPage
            }

            function searchResult() {
                proxy.$axios
                    .get('/search?keywords=' + proxy.$refs.searchBar.keywords, {}).then(resp => {
                    if (resp && resp.data.code === 200) {
                        data.books = resp.data.result
                    }
                })
            }

            return {
                ...toRefs(data),
                loadBooks,
                onMounted,
                searchResult,
                handleCurrentChange
            }

        }


    }
</script>
<style scoped>

    .cover {
        width: 115px;
        height: 172px;
        margin-bottom: 7px;
        overflow: hidden;
        cursor: pointer;
    }

    img {
        width: 115px;
        height: 172px;
        /*margin: 0 auto;*/
    }

    .title {
        font-size: 14px;
        text-align: left;
    }

    .author {
        color: #333;
        width: 102px;
        font-size: 13px;
        margin-bottom: 6px;
        text-align: left;
    }

    .abstract {
        display: block;
        line-height: 17px;
    }

    .el-icon-delete {
        cursor: pointer;
        float: right;
    }

    .switch {
        display: flex;
        position: absolute;
        left: 780px;
        top: 25px;
    }

    a {
        text-decoration: none;
    }

    a:link, a:visited, a:focus {
        color: #3377aa;
    }

</style>
